/**
 * related to login.ui
 * 
 * @Author : lily80239464
 * @Timestamp : 2017-11-09
 */
var do_Page = sm("do_Page");
var do_App = sm("do_App");
var anim = require("anim");
var nf = sm("do_Notification");
var do_DataCache = sm("do_DataCache");//sp
var storage = sm("do_Storage");
var buttonA = mm("do_Animation", "BUTTONTOUCHDOWNS", "do_App");

//订阅android系统返回键的时间；关闭当前页面
do_Page.on("back", function() {
	do_App.closePage();
});

ui("do_Button_setnet").on("touch", function() {
	do_App.openPage({source: "source://view/setting/setting.ui", animationType:"push_t2b", statusBarState:"transparent"});
});
var username = ui("do_TextField_username");
var password = ui("do_TextField_password");
ui("do_ALayout_login").on("touch",function() {
	username.setFocus(false);
	password.setFocus(false);
	do_Page.hideKeyboard();
});
//登录按下状态
var animLogYes = mm("do_Animator");
var propsLY = {
	width : 120,
	x : 240,
	bgColor : "A6EBFFFF"
};
animLogYes.append(360, propsLY, "EaseOut");
// 恢复状态
var animLogB = mm("do_Animator");
var propsB = {
	width : 580,
	x : 10,
	bgColor : "3BD1FFFF"
};
animLogB.append(360, propsB, "EaseOut");
do_Page.on("result", function(data, e) {
	if (do_DataCache.hasData("baseUrl")) {
		baseUrl = do_DataCache.loadData("baseUrl");
	}else {
		baseUrl = "http://125.74.207.139:8010/webservice/a.asmx/";
		do_DataCache.saveData("baseUrl", baseUrl);
	}
	deviceone.print(baseUrl);
});

var baseUrl;
if (do_DataCache.hasData("baseUrl")) {
	baseUrl = do_DataCache.loadData("baseUrl");
}else {
	baseUrl = "http://125.74.207.139:8010/webservice/a.asmx/";
	do_DataCache.saveData("baseUrl", baseUrl);
}
var progressBar = ui("do_ProgressBar_progress");
progressBar.visible = false;
var loginBtn = ui("do_Button_login");
loginBtn.on("touch", function(data, e) {
	progressBar.visible =true;
	loginBtn.animate(buttonA);
	if (username.text == "" && password.text == "") {
		nf.toast("输入账号|密码");
		username.setFocus(true);
	} else if (username.text == "") {
		nf.toast("输入账号");
		username.setFocus(true);
	} else if (password.text == "") {
		nf.toast("输入密码");
		password.setFocus(true);
	}
	var http = mm("do_Http");
	http.method = "GET";
	http.timeout = 10000;
	http.contentType = "application/x-www-form-urlencoded";
	http.url = baseUrl+"login?username="+encodeURI(username.text)+"&password="+password.text;
	deviceone.print(http.url);
	http.on("success", function(data, e) {
		var jsonString = data.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=\"http://tempuri.org/\">","");
		jsonString = jsonString.replace("</string>","");
		deviceone.print(jsonString);
		var bean = JSON.parse(jsonString);
		deviceone.print(bean.result);
		if (bean.result==200) {
			nf.toast("登录成功");
			do_DataCache.saveData("loginer", bean);
			do_App.closePage();
			do_App.openPage({source:"source://view/index.ui",animationType:"push_r2l",  statusBarState:"transparent"});
		}else {
			nf.toast(bean.msg);
		}
		progressBar.visible =false;
	});
	http.on("fail", function(data, e) {
		deviceone.print(JSON.stringify(data));
		if (data.status == 404) {
			nf.toast("用户名或密码错误");
		} else {
			nf.toast("网络问题");
		}
		progressBar.visible = false;
		
	});
	http.request();
});

